package com.google.fbyoutube.client.widget;

import java.util.HashMap;
import java.util.Map;

import com.google.fbyoutube.client.YouTube;
import com.google.fbyoutube.shared.FeedUrlMaker;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PalSortBy extends Composite {

	private String sortBy = FeedUrlMaker.MOST_POPULAR_FEED;
	private String category = FeedUrlMaker.DEFAULT_CAT;
	private String time = FeedUrlMaker.DefaultDueTime;
	private String start = FeedUrlMaker.DEFAULT_START_INDEX;
	private String maxResult = FeedUrlMaker.DEFAULT_MAX_RESULTS;
	private HorizontalPanel pagingNav;
	public WidSearchBar searchBar;

	public PalSortBy() {

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setSpacing(2);
		horizontalPanel.addStyleName("navbar");
		initWidget(horizontalPanel);
		horizontalPanel.setSize("246px", "25px");

		final DecoratedPopupPanel decoratedTypePopup = new DecoratedPopupPanel();
		final DecoratedPopupPanel decoratedTimePopup = new DecoratedPopupPanel();
		final DecoratedPopupPanel decoratedCatPopup = new DecoratedPopupPanel();
		decoratedCatPopup.setAutoHideEnabled(true);

		decoratedTypePopup.setAutoHideEnabled(true);

		final Button btnMostViewed = new Button("Most Popular");
		horizontalPanel.add(btnMostViewed);
		btnMostViewed.setHeight("35px");

		btnMostViewed.addMouseOverHandler(new MouseOverHandler() {

			@Override
			public void onMouseOver(MouseOverEvent event) {
				Button btn = (Button) event.getSource();
				int clientx = btn.getAbsoluteLeft();
				int clienty = btn.getElement().getAbsoluteBottom();
				decoratedTypePopup.setPopupPosition(clientx, clienty);
				decoratedTypePopup.show();
				decoratedCatPopup.hide();
				decoratedTimePopup.hide();
			}
		});
		
		btnMostViewed.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				Button btn = (Button) event.getSource();
				int clientx = btn.getAbsoluteLeft();
				int clienty = btn.getElement().getAbsoluteBottom();
				decoratedTypePopup.setPopupPosition(clientx, clienty);
				decoratedTypePopup.show();
				decoratedCatPopup.hide();
				decoratedTimePopup.hide();
			}
		});
		
		VerticalPanel verticalPanel_2 = new VerticalPanel();
		verticalPanel_2.setStyleName("category-box");
		verticalPanel_2.setSpacing(5);
		decoratedTypePopup.add(verticalPanel_2);

		for (final String key : FeedUrlMaker.typeSpec.keySet()) {
			if (key != null) {
				final HTML videoCat = new HTML(key, true);
				videoCat.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						sortBy = FeedUrlMaker.typeSpec.get(key);
						resetStartIndex();
						btnMostViewed.setText(videoCat.getText());
						Map<String, String> options = new HashMap<String, String>();
						options.put(FeedUrlMaker.TIME, time);
						options.put(FeedUrlMaker.START_INDEX, start);
						options.put(FeedUrlMaker.MAX_RESULTS, maxResult);
						YouTube.getAppGUI().resetVideoList(sortBy, category,
								null, options);
						decoratedTypePopup.hide();
					}
				});

				videoCat.setStyleName("category-item");
				verticalPanel_2.add(videoCat);
			}
		}

		final Button btnNewButton = new Button("All Categories");
		horizontalPanel.add(btnNewButton);
		btnNewButton.setHeight("35px");
		btnNewButton.addMouseOverHandler(new MouseOverHandler() {

			@Override
			public void onMouseOver(MouseOverEvent event) {
				Button btn = (Button) event.getSource();
				int clientx = btn.getAbsoluteLeft();
				int clienty = btn.getElement().getAbsoluteBottom();
				decoratedCatPopup.setPopupPosition(clientx, clienty);
				decoratedCatPopup.show();
				decoratedTypePopup.hide();
				decoratedTimePopup.hide();

			}
		});
		
		btnNewButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				Button btn = (Button) event.getSource();
				int clientx = btn.getAbsoluteLeft();
				int clienty = btn.getElement().getAbsoluteBottom();
				decoratedCatPopup.setPopupPosition(clientx, clienty);
				decoratedCatPopup.show();
				decoratedTypePopup.hide();
				decoratedTimePopup.hide();

			}
		});
		
		VerticalPanel verticalPanel_0 = new VerticalPanel();
		verticalPanel_0.setStyleName("category-box");
		verticalPanel_0.setSpacing(5);
		verticalPanel_0.setSize("120px", "354px");

		for (final String key : FeedUrlMaker.categorySpec.keySet()) {
//			if (key != null) {	// Null if fine
				final HTML videoCat = new HTML(FeedUrlMaker.categorySpec.get(key),
						true);
				videoCat.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						category = key;
						resetStartIndex();
						btnNewButton.setHTML(videoCat.getText());
						Map<String, String> options = new HashMap<String, String>();
						options.put(FeedUrlMaker.TIME, time);
						options.put(FeedUrlMaker.START_INDEX, start);
						options.put(FeedUrlMaker.MAX_RESULTS, maxResult);
						YouTube.getAppGUI().resetVideoList(sortBy, category,
								null, options);
						decoratedCatPopup.hide();
					}
				});

				videoCat.setStyleName("category-item");
				verticalPanel_0.add(videoCat);
//			}
		}

		decoratedCatPopup.setWidget(verticalPanel_0);
		verticalPanel_0.setSize("100%", "100%");

		final Button btnNewButton_1 = new Button("Today");
		horizontalPanel.add(btnNewButton_1);
		btnNewButton_1.setHeight("35px");
		btnNewButton_1.addMouseOverHandler(new MouseOverHandler() {

			@Override
			public void onMouseOver(MouseOverEvent event) {
				Button btn = (Button) event.getSource();
				int clientx = btn.getAbsoluteLeft();
				int clienty = btn.getElement().getAbsoluteBottom();
				decoratedTimePopup.setPopupPosition(clientx, clienty);
				decoratedTimePopup.show();
				decoratedCatPopup.hide();
				decoratedTypePopup.hide();
			}
		});
		
		btnNewButton_1.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				Button btn = (Button) event.getSource();
				int clientx = btn.getAbsoluteLeft();
				int clienty = btn.getElement().getAbsoluteBottom();
				decoratedTimePopup.setPopupPosition(clientx, clienty);
				decoratedTimePopup.show();
				decoratedCatPopup.hide();
				decoratedTypePopup.hide();
			}
		});
		
		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setStyleName("category-box");
		verticalPanel.setSpacing(5);

		for (final String key : FeedUrlMaker.timeSpec.keySet()) {
			if (key != null) {
				final HTML videoCat = new HTML(key, true);
				videoCat.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						time = FeedUrlMaker.timeSpec.get(key);
						resetStartIndex();
						btnNewButton_1.setHTML(videoCat.getText());
						Map<String, String> options = new HashMap<String, String>();
						options.put(FeedUrlMaker.TIME, time);
						options.put(FeedUrlMaker.START_INDEX, start);
						options.put(FeedUrlMaker.MAX_RESULTS, maxResult);
						YouTube.getAppGUI().resetVideoList(sortBy, category,
								null, options);
						decoratedTimePopup.hide();
					}
				});

				videoCat.setStyleName("category-item");
				verticalPanel.add(videoCat);
			}
		}

		decoratedTimePopup.add(verticalPanel);
		decoratedTimePopup.setAutoHideEnabled(true);

		// Search bar
		SimplePanel placeHolder = new SimplePanel();
		placeHolder.setWidth("50px");
		horizontalPanel.add(placeHolder);

		searchBar = new WidSearchBar();
		horizontalPanel.add(searchBar);
		
		pagingNav = new HorizontalPanel();
        final Button prevButton = new Button("<");
        prevButton.setHeight("35px");
        prevButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				start = String.valueOf((Integer.parseInt(start) - Integer.parseInt(maxResult)));
				if (Integer.parseInt(start) <= 0) { resetStartIndex(); }
				if (Integer.parseInt(start) == Integer.parseInt(FeedUrlMaker.DEFAULT_START_INDEX)) {
		        	prevButton.setVisible(false);
		        } else {
		        	prevButton.setVisible(true);
		        }
				Map<String, String> options = new HashMap<String, String>();
//				options.put(FeedUrlMaker.TIME, time);
				options.put(FeedUrlMaker.START_INDEX, start);
				if (searchBar.getInputText() != null) {
					options.put("q", searchBar.getInputText());
				}
				YouTube.getAppGUI().resetVideoList(null, null,
						null, options);
			}
		});
        pagingNav.add(prevButton);
        Button nextButton = new Button(">");
        nextButton.setHeight("35px");
        nextButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				start = String.valueOf((Integer.parseInt(start) + Integer.parseInt(maxResult)));
		        prevButton.setVisible(true);
				Map<String, String> options = new HashMap<String, String>();
//				options.put(FeedUrlMaker.TIME, time);
				options.put(FeedUrlMaker.START_INDEX, start);
				if (searchBar.getInputText() != null) {
					options.put("q", searchBar.getInputText());
				}
				YouTube.getAppGUI().resetVideoList(null, null,
						null, options);
			}
		});
        pagingNav.add(nextButton);
        
        if (start == FeedUrlMaker.DEFAULT_START_INDEX) {
        	prevButton.setVisible(false);
        } else {
        	prevButton.setVisible(true);
        }
        
        SimplePanel placeHolder2 = new SimplePanel();
		placeHolder2.setWidth("50px");
		horizontalPanel.add(placeHolder2);
		
		horizontalPanel.add(pagingNav);
		pagingNav.setVisible(false);
	}
	
	private void resetStartIndex() {
		start = FeedUrlMaker.DEFAULT_START_INDEX;
	}

	public void setShowPaging(boolean flag) {
		pagingNav.setVisible(flag);
	}
}
